// pages/mine/mine.js
const app = getApp({})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2024-06-07', // 初始日期，可根据需要设定
    showVipFlag:0,
    vipNum:"",
    userInfo:{
      userName:"登录",
      loginTime:"让学习更高效",
      vip:0
    },
    vipTitle:"外部用户",
    vipContLine1:"外部多项权益,需要组织内部账号请联系组织管理员！",
    vipContLine2:"点击下方组织管理员进行联系",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(wx.getStorageSync('userInfo')){
        this.setData({
            userInfo:{
                userName:wx.getStorageSync('userInfo').username,
                loginTime:wx.getStorageSync('userInfo').loginTime,
                vip:wx.getStorageSync('userInfo').vip
            }
        })
        if(wx.getStorageSync('userInfo').vip==1){
            this.setData({
                vipTitle:"会员用户",
                vipContLine1:"您的会员开始时间是"+wx.getStorageSync('userInfo').created_at,
                vipContLine2:"会员到期时间"+wx.getStorageSync('userInfo').vipend,
            })
        }
    }
  },
  /* VIP激活码输入 */
  vipNumIn(e){
    this.setData({
      vipNum:e.detail.value
    })
  },
  
  /* 弹窗展示 */
  showVipView(){
    this.setData({
      showVipFlag:!this.data.showVipFlag
    })
    console.log(this.data.showVipFlag);
  },
  none(e){
  },
  /* 登录功能 */
  bindUser(){
      if(wx.getStorageSync('userInfo')){
          wx.showToast({
            title: '您已登录',
            icon:"none"
          })
          return
      }
    wx.navigateTo({
      url: '/pages/login/login',
    })
     
  },
  /* 目标时间确定 */
  bindDateChange: function(e) {
    this.setData({
        date: e.detail.value
    });
   var result = this.judgeLogin() //判断登录
   if(result == false){
      return
   }
    //设定目标
    wx.showModal({
      title: '填写一个目标标题,比如2025专升本',
      content: '',
      editable:true,
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '取消选择',
            icon:'none'
        })
        }
        if (res.confirm) {
            if(! wx.getStorageSync('target')){
                var temp = {}
                temp.date = e.detail.value
                temp.title = res.content
                wx.setStorageSync('target', temp)
            }if(wx.getStorageSync('target')){

                wx.getStorageSync('target').date=e.detail.value
                wx.getStorageSync('target').title = res.content
            }
            wx.showToast({
              title: '更改成功',
            })
            wx.reLaunch({
               url: '/pages/index/index',
            })
            console.log('日期更新成功');
        }
      }
    })
    
 },
 /* 判断登录的标志 */
 judgeLogin(){
     
     if(!wx.getStorageSync('userInfo')){
         wx.showToast({
           title: '请先登录',
           icon:'none'
         })
         return false
     }
 },
 /* 退出登录 */
 exitLogin(){
     wx.showModal({
       title: '确定退出？',
       complete: (res) => {
         if (res.confirm) {
           wx.clearStorageSync()
           wx.reLaunch({
             url: '/pages/mine/mine',
           })
         }
       }
     })
 },
 /* 联系客服 */
 contactKefu(){
     wx.navigateTo({
       url: '/pages/server/server',
     })
 },
 /* 更多学习记录 */
 moreStudyDounment(){
    if(!wx.getStorageSync('userInfo')){
        wx.showToast({
          title: '请先登录',
          icon:'none'
        })
    }else{
        wx.navigateTo({
          url: '/pages/studyMore/studyMore',
        })
    }
},
/* 激活码 */
vipNumIn(e){
    console.log(e.detail.value);
    this.setData({
        jihuoCode:e.detail.value
    })
},
/* 确定激活 */
confirmCode(){
    wx.showLoading({
        title:'检查中'
    })
    console.log(this.data.jihuoCode);
    wx.request({
      url:app.globalData.baseUrl+'/user/activate_code',
      method:"POST",
      data:{
        code:this.data.jihuoCode,
        userid:wx.getStorageSync('userInfo').id
      },success:(res)=>{
         console.log(res);
         wx.hideLoading()
         this.setData({
            showVipFlag:!this.data.showVipFlag
          })
         if(res.data.success==true){
             wx.showModal({
               title: '激活码兑换成功',
               content: '点击确定退出登录更新会员状态',
               complete: (res) => {
                 if (res.cancel) {
                    wx.showToast({
                      title: '记得退出哦~',
                    })
                 }
                 if (res.confirm) {
                   this.exitLogin()
                 }
               }
             })
         }else if(res.data.success=="error"){
            wx.showToast({
              title: '激活码错误',
              icon:"none"
            })
         }else{
            wx.showToast({
                title: 'SERVER ERROR',
                icon:"none"
            })
         }
      },fail:(res)=>{
          wx.hideLoading()
          wx.showToast({
            title: 'SERVER ERROR',
            icon:"none"
          })
      }
    })
},
/* 错题本 */
mistake(){
    if(this.judgeLogin==false){
        wx.showToast({
          title: '请先登录',
          icon:'none'
        })
        return
    }
    wx.navigateTo({
      url: '/pages/mistake/mistake',
    })
}
,
/* 收藏夹 */
collect(){
    if(this.judgeLogin==false){
        wx.showToast({
          title: '请先登录',
          icon:'none'
        })
        return
    }
    wx.navigateTo({
      url: '/pages/coll/coll'
    })
},
noteBook(){
    if(this.judgeLogin==false){
        wx.showToast({
          title: '请先登录',
          icon:'none'
        })
        return
    }
    wx.navigateTo({
      url: '/pages/note/note'
    })
},
navlogin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
}

})